package com.screen.screendisplay.service.impl.electric;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.screen.screendisplay.entity.electric.ElectricProdoperCoalConsumeUr;
import com.screen.screendisplay.mapper.electric.ElectricProdoperCoalConsumeUrMapper;
import com.screen.screendisplay.service.electric.ElectricProdoperCoalConsumeUrService;
import com.screen.screendisplay.vo.electric.ElectricityRateDto;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 综合厂用电率表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class ElectricProdoperCoalConsumeUrServiceImpl extends ServiceImpl<ElectricProdoperCoalConsumeUrMapper, ElectricProdoperCoalConsumeUr> implements ElectricProdoperCoalConsumeUrService {

    /**
     * 综合厂用电率
     * @param year 年份
     * @return Map包含：yearAverage(全年平均用电率)、yoyChange(同比增加)、dataList(实体集合)
     */
    @Override
    public Map<String, Object> prodoperCoalConsumeUr(Integer year) {
        // 如果用户没有传入year参数，默认查询当前年份
        if (year == null) {
            year = LocalDate.now().getYear();
        }
        
        // 查询指定年份的数据，并按日期升序排列
        List<ElectricProdoperCoalConsumeUr> currentYearData = this.lambdaQuery()
                .eq(ElectricProdoperCoalConsumeUr::getYear, year)
                .orderByAsc(ElectricProdoperCoalConsumeUr::getRecordDate)
                .list();
        
        // 查询上一年的数据，用于计算同比增加
        List<ElectricProdoperCoalConsumeUr> lastYearData = this.lambdaQuery()
                .eq(ElectricProdoperCoalConsumeUr::getYear, year - 1)
                .list();
        
        // 转换为DTO对象列表
        List<ElectricityRateDto> dataList = currentYearData.stream()
                .map(item -> {
                    ElectricityRateDto dto = new ElectricityRateDto();
                    dto.setYear(item.getYear());
                    dto.setMonth(item.getMonth());
                    dto.setRecordDate(item.getRecordDate());
                    dto.setElectricityRate(item.getElectricityRate());
                    return dto;
                })
                .collect(Collectors.toList());
        
        // 计算全年平均用电率
        BigDecimal yearAverage = BigDecimal.ZERO;
        if (!currentYearData.isEmpty()) {
            BigDecimal sum = currentYearData.stream()
                    .filter(item -> item.getElectricityRate() != null)
                    .map(ElectricProdoperCoalConsumeUr::getElectricityRate)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            
            long count = currentYearData.stream()
                    .filter(item -> item.getElectricityRate() != null)
                    .count();
            
            if (count > 0) {
                yearAverage = sum.divide(BigDecimal.valueOf(count), 4, RoundingMode.HALF_UP);
            }
        }
        
        // 计算同比增加
        BigDecimal yoyChange = BigDecimal.ZERO;
        if (!lastYearData.isEmpty() && !currentYearData.isEmpty()) {
            // 计算去年平均用电率
            BigDecimal lastYearSum = lastYearData.stream()
                    .filter(item -> item.getElectricityRate() != null)
                    .map(ElectricProdoperCoalConsumeUr::getElectricityRate)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            
            long lastYearCount = lastYearData.stream()
                    .filter(item -> item.getElectricityRate() != null)
                    .count();
            
            if (lastYearCount > 0) {
                BigDecimal lastYearAverage = lastYearSum.divide(BigDecimal.valueOf(lastYearCount), 4, RoundingMode.HALF_UP);
                
                if (lastYearAverage.compareTo(BigDecimal.ZERO) > 0) {
                    // 同比增长率 = (今年平均 - 去年平均) / 去年平均 * 100
                    yoyChange = yearAverage.subtract(lastYearAverage)
                            .divide(lastYearAverage, 4, RoundingMode.HALF_UP)
                            .multiply(BigDecimal.valueOf(100));
                }
            }
        }
        
        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("yearAverage", yearAverage);
        result.put("yoyChange", yoyChange);
        result.put("dataList", dataList);
        
        return result;
    }

}